let tree = [
    {
        "id": 1,
        "pid": 0,
        "name": "body",
        "children": [
            {
                "id": 2,
                "pid": 1,
                "name": "title",
                "children": [
                    {
                        "id": 3,
                        "pid": 2,
                        "name": "div"
                    }
                ]
            },
            {
                "id": 4,
                "pid": 1,
                "name": "script"
            }
        ]
    }
]
let res = []
function TreeFlat(tree) {
    if (!tree) return
    tree.forEach(item => {
        res.push({
            "id": item.id,
            "pid": item.pid,
            "name": item.name,
        })
        TreeFlat(item.children)
    })
}

TreeFlat(tree)
console.log(res);